package Dynamic_programming;

import org.junit.Test;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

import java.io.File;
import java.util.*;

/**
 * @Description: $
 * @Param: $
 * @return: $
 * @Author 万家欣
 * @Date: 2022/7/11
 * Algorithm
 * @Version 1.0
 * library(readxl)
 *  mydata <-read_excel("D:/a.xlsx",sheet=1,na="NA")
 * library(openair)
 * library(tidyverse)
 * png(file="C:/Users/1035/Desktop/myplot.png",res = 150)
 * polarPlot(mydata, pollutant = "COL2",statistic = "nwr")
 * dev.off();
 */
public class JavaCallRDemo {


      /**
        * 删除单个文件
       * @param   sPath    被删除文件的文件名
        * @return 单个文件删除成功返回true，否则返回false
        */
      public boolean deleteFile(String sPath) {
           boolean  flag = false;
            File file = new File(sPath);
             // 路径为文件且不为空则进行删除
             if (file.isFile() && file.exists()) {
                     file.delete();
                     flag = true;
                 }
             return flag;
              }

    @Test
    public void JavaCallRDemo(){
        List<Map<String,Double>> listMap = new ArrayList<>();
          List<String> list = Arrays.asList("COL2","wd","ws");
          List<Double> list1 = Arrays.asList(8.0, 12.0, 10.0, 12.0, 14.0, 16.0, 14.0, 13.0, 16.0, 13.0, 14.0, 10.0, 13.0, 14.0, 13.0, 17.0, 22.0, 20.0);
        List<Double> list2 = new ArrayList<>();
        List<Double> list3 = new ArrayList<>();
        //   List<Double> list2 = Arrays.asList(38.586 ,29.592 ,33.548 ,18.779 ,41.362 ,49.275 ,89.559 ,8.494 ,71.641 ,171.16 ,126.916 ,116.032 ,72.01 ,42.498 ,35.481 ,31.989 ,37.235 ,72.177);
         /// List<Double> list3 = Arrays.asList(0.64 ,0.447 ,0.488 ,0.733 ,0.627 ,0.531 ,0.9 ,0.398 ,0.387 ,0.727 ,0.895 ,0.125 ,0.71 ,1.121 ,1.033 ,0.99 ,1.12 ,1.212);
        String sPath = "D:/a.xls";

        for (int i = 0; i < list1.size(); i++) {
            Map<String,Double> map = new HashMap<>();
            map.put("COL2",list1.get(i));map.put("wd",null);map.put("ws",null);
            listMap.add(map);
        }
        LinkedHashMap<String,String> map1 = new LinkedHashMap<>();
        map1.put("COL2","COL2"); map1.put("wd","wd"); map1.put("ws","ws");
      // ExcelUtil.exportToExcel(list.toArray(new String[]{}),"sheet1",listMap,map1,sPath);、
            String key = "COL2";String name = "asda";
        String code = "554";String unit ="nox (ug/m3)";
        ExcelUtil.createExcel(listMap,list.toArray(new String[]{}),sPath);

        RConnection c = null;
        try{
            c = new RConnection();
            c.assign("spath",sPath);

            c.eval("library(readxl)");//Java执行R命令
            c.eval("mydata <-read_excel(spath,sheet=1,na=\"NA\")");
            c.eval("library(openair)");
            c.eval("library(tidyverse)");
            c.assign("pathfile","C:/Users/1035/Desktop/"+code+".png");
            c.assign("factor","COL2");
            c.eval("png(file=pathfile,res = 150)");
            c.assign("www",key);
            c.assign("ttt",name);
            c.assign("unit",unit);
            c.eval("polarPlot(mydata, pollutant = www,statistic = \"nwr\",main = ttt,key.header =unit)");
            c.eval("dev.off();");
            deleteFile(sPath);
        } catch (RserveException e) {
            e.printStackTrace();
        } finally {
            c.close();
        }
    }
}
